//
//  MembersViewController.m
//  zhixinApp
//
//  Created by zhiguanzhl on 2017/11/19.
//  Copyright © 2017年 zhiguanzhl. All rights reserved.
//

#import "MembersViewController.h"
#import "MemlistTableViewCell.h"
#import "SelectMemberViewController.h"

@interface MembersViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong)UITableView *tableView;
@property (nonatomic,strong)NSMutableArray *reducePeople;

@end

@implementation MembersViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"群成员";
    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kSCREEN_WIDTH, kSCREEN_HEIGHT-64) style:UITableViewStylePlain];
    self.tableView = tableView;
    tableView.delegate = self;
    tableView.dataSource = self;
    if (_notAdmin) {
        //不是管理员只能看
        tableView.tableFooterView = [[UIView alloc] init];
    }else{
        
         tableView.tableFooterView = [self BottmView];
    }
    
   
    [self.view addSubview:tableView];
    
//    [self _setupBarButtonItem];
}

//- (void)_setupBarButtonItem
//{
//
//    UIButton *addButton = [UIButton buttonWithType:UIButtonTypeCustom];
//
//    addButton.frame = CGRectMake(0, 0, 44, 44);
//
//    [addButton setTitle:@"+" forState:(UIControlState)UIControlStateNormal];
//    [addButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
//    [addButton addTarget:self action:@selector(addAction) forControlEvents:UIControlEventTouchUpInside];
//    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:addButton];
//
//}
//- (void)addAction{
//
//    SelectMemberViewController *selectVc = [[SelectMemberViewController alloc] init];
//    selectVc.conversationId = self.conversationId;
//
//    selectVc.logiaIdArr = self.logaiNameArr;
//    [self.navigationController pushViewController:selectVc animated:YES];
//
//}

- (UIView *)BottmView{
    
    UIView *footView= [[UIView alloc]initWithFrame:CGRectMake(0,0, kSCREEN_WIDTH, 72)];
    [self.view addSubview:footView];
    
    UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(15, 10, kSCREEN_WIDTH-30, 52)];
    btn.layer.cornerRadius = 52*0.5;
    btn.layer.masksToBounds = YES;
    btn.backgroundColor = [UIColor colorWithHexString:@"2c9ae8"];
    btn.titleLabel.font = [UIFont systemFontOfSize:18];
    [btn setTitle:@"确认" forState:UIControlStateNormal];
    [btn setTitleColor:KWhiteColor forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(sureReduce) forControlEvents:UIControlEventTouchUpInside];
    [footView addSubview:btn];
    
    return footView;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
//    return self.memberArr.count;
    return self.modalsArr.count;

}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 70;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MemlistTableViewCell *membersCell = [MemlistTableViewCell cellWithTableView:tableView];
    SelectModel *model = self.modalsArr[indexPath.row];
//    membersCell.nickLabel.text = self.memberArr[indexPath.row];
//    NSString *name = self.memberArr[indexPath.row];
    membersCell.nickLabel.text = model.name;
        NSString *name = model.name;
    if (name.length>2) {
        name = [name substringFromIndex:1];
    }
    membersCell.headerButton.text = name;
    
    return membersCell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (_notAdmin) {
        
        iToast *toast = [[iToast alloc] initWithText:@"您不是管理员，只能查看成员"];
        [toast show];
        
    }else{
        
        if (self.conversationId) {
            
            //移除群组成员
//            [[EMClient sharedClient].groupManager removeOccupants:@[self.logaiNameArr[indexPath.row]] fromGroup:self.conversationId error:nil];
            SelectModel *model = self.modalsArr[indexPath.row];
            [[EMClient sharedClient].groupManager removeOccupants:@[model.loginName] fromGroup:self.conversationId error:nil];
        }
//        [self.memberArr removeObjectAtIndex:indexPath.row];
//        [self.logaiNameArr removeObjectAtIndex:indexPath.row];
        [self.modalsArr removeObjectAtIndex:indexPath.row];
        
        [self.tableView reloadData];
    }
    
}

- (void)sureReduce{
    
    if (self.block) {
//        self.block(self.memberArr, self.logaiNameArr);
        self.block(self.modalsArr);
    }
    
    [self.navigationController popViewControllerAnimated:YES];
    
}


//- (NSMutableArray *)memberArr{
//    if (!_memberArr) {
//        _memberArr = [[NSMutableArray alloc] init];
//    }
//    return _memberArr;
//}

- (NSMutableArray *)modalsArr{
    if (!_modalsArr) {
        _modalsArr = [[NSMutableArray alloc] init];
    }
    return _modalsArr;
}
@end
